# Makefile for the simple OS from scratch project

C_SOURCES = $(wildcard kernel/*.c drivers/*.c)
HEADERS = $(wildcard kernel/*.h drivers/*.h)
OBJ = ${C_SOURCES:.c=.o}

all: os_image.bin

run: all
	qemu-system-x86_64 -curses os_image.bin

clean: 
	rm -fr *.bin *.o
	rm -fr kernel/*.o boot/*.bin drivers/*.o

os_image.bin: boot/boot_sect.bin kernel/kernel.bin
	cat $^ > os_image.tmp
	dd if=/dev/zero ibs=1k count=100 > $@
	dd if=os_image.tmp of=$@ conv=notrunc
	rm os_image.tmp

kernel/kernel.bin: kernel/kernel_entry.o ${OBJ}
	ld -o $@ -Ttext 0x1000 $^ --oformat binary

%.o: %.c ${HEADERS}
	gcc -ffreestanding -Iinclude -c $< -o $@

%.o: %.asm
	nasm $< -f elf64 -o $@

%.bin: %.asm
	nasm $< -f bin -I 'boot/inc/' -o $@

#boot_sect.bin: boot_sect.asm
#	nasm $< -f bin -I 'boot/inc/' -o $@

#kernel.bin: kernel/kernel_entry.o ${OBJ}
#	ld -o $@ -Ttext 0x1000 $^ --oformat binary

#kernel_entry.o: kernel_entry.asm
#	nasm $< -f elf64 -o $@

#kernel.o: kernel.c
#	gcc -ffreestanding -c $< -o $@

